﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Controls.Controls;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public class MainViewModel : ViewModelBase
    { 
        public string year = "", month = "", day = "", date = "";
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public SETINTEXDB SetEnv
        {
            get;
            set;

        }
        public SETINTEXDB SetProd
        {
            get;
            set;
        }
        public SETINTEXDB SetData
        {
            get;
            set;
        }
        private string _KRS; public string KRS { get { return _KRS; } set { _KRS = value; OnPropertyChanged("KRS"); } }
        private string _LFD; public string LFD { get { return Helper.Right("000000" + _LFD, 6); } set { _LFD = value; OnPropertyChanged("LFD"); } }
        private string _POS; public string POS { get { return Helper.Right("000" + _POS, 3); ; } set { _POS = value; OnPropertyChanged("POS"); } }
        private string _ABKRS; public string ABKRS { get { return _ABKRS; } set { _ABKRS = value; OnPropertyChanged("ABKRS"); } }
        private string _ABLFD; public string ABLFD { get { return Helper.Right("000000" + _ABLFD, 6); } set { _ABLFD = value; OnPropertyChanged("ABLFD"); } }
        private string _ABPOS; public string ABPOS { get { return Helper.Right("000" + _ABPOS, 3); ; } set { _ABPOS = value; OnPropertyChanged("ABPOS"); } }
        private string _MKFTP; public string MKFTP { get { return _MKFTP; } set { _MKFTP = value; OnPropertyChanged("MKFTP"); } }

        private string _KSTKRS; public string KSTKRS { get { return _KSTKRS; } set { _KSTKRS = value; OnPropertyChanged("KSTKRS"); } }
        private string _KST; public string KST { get { return _KST; } set { _KST = value; OnPropertyChanged("KST"); } }
        private string _AGANG; public string AGANG { get { return _AGANG; } set { _AGANG = value; OnPropertyChanged("AGANG"); } }
        private string _AMTYP; public string AMTYP { get { return _AMTYP; } set { _AMTYP = value; OnPropertyChanged("AMTYP"); } }
        private DataRow m_dwVKP = null;
        public DataRow dwVKP
        {
            get
            {
                return m_dwVKP;
            }
            set
            {
                m_dwVKP = value;
                _KRS = dwVKP["VKPAUFKRS"].ToString();
                _LFD = dwVKP["VKPAUFLFD"].ToString();
                _POS = dwVKP["VKPAUFPOS"].ToString();
                OnPropertyChanged("dwVKP");
            }
        }

        public MainViewModel()
            : base()
        {
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            if (SetData == null)
            {
                SetData = new SETINTEXDB();
            }
            if (SetProd == null)
            {
                SetProd = new SETINTEXDB();
            }
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public void LoadVKPList()
        {
            try
            {
                DataSet SResult = new DataSet();
                DataRow dwData = SetEnv.ICC_QUERYTB.NewRow();
                dwData["VKPMANDANT"] = app.Mandant;
                dwData["VKPAUFKRS"] = _KRS;
                dwData["VKPAUFLFD"] = _LFD;
                SResult = INTEXDBC.QueryICC_PVKPOS(dwData, 3);
                SResult.Tables[0].TableName = "VKPOS";
                SetEnv.VKPOS.Clear();
                SetEnv.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void LoadEnv()
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData;

            dwData = SetEnv.AMSTAMM.NewRow();
            dwData["AMMANDANT"] = "00";
            dwData["AMKSTKRS"] = KSTKRS;
            dwData["AMKST"] = KST;
            dwData["AMTYP"] = AMTYP;
            SResult = INTEXDBC.QueryAMSTAMM(dwData, 1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["AMBEZ"] = Helpers.Helper.GetGB2312Value(row["AMBEZ"].ToString().Trim());
                row["AMAENDZL"] = 1;//排产最后日期
                row["AMAENDDAT"] = 0;//排产最后日期
                row["AMNEUDAT"] = "0000";//排产最后时间
            }
            SetEnv.Merge(SResult);
            INTEXDBC.DeleteICC_STDMACHINE(null, 1);
            INTEXDBC.InsertICC_STDMACHINE(null, 1);
            SResult = INTEXDBC.QueryICC_STDMACHINE(dwData, 1);
            SetEnv.Merge(SResult);
            SResult = INTEXDBC.QueryICC_SRULES(null, 9999);
            SetEnv.Merge(SResult);
            SResult = INTEXDBC.QueryICC_PLANSETUP(null, 9999);
            SetEnv.Merge(SResult);

        }
        //寻找出所有的生产单号码，可以一次得到
        private DataSet YGRON(string _krs, string _lfd, string _pos, string _EXART)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataSet STemp = new DataSet();
            DataRow dwData = SetEnv.ICC_QUERYTB.NewRow();
            dwData["ABAVKAUFKRS"] = _krs;
            dwData["ABAVKAUFLFD"] = _lfd;
            dwData["ABAVKAUFPOS"] = _pos;
            SResult = INTEXDBC.QueryICC_ABAX(dwData, 4);
            SReturn.Merge(SResult);
            dwData["ABFMANDANT"] = app.Mandant;
            dwData["ABFAUFKRS"] = _krs;
            dwData["ABFAUFLFD"] = _lfd;
            dwData["ABFAUFPOS"] = _pos;
            SResult = INTEXDBC.QueryICC_MKRF(dwData, 6);
            SReturn.Merge(SResult);
            string key = _krs + _lfd + _pos + ";";
            string processed = key;
            bool IsRun = false;
            while (true)
            {
                IsRun = false;
                foreach (DataRow row in SReturn.Tables["ICC_ABAX"].Rows)
                {
                    string ABAAUFTYP = row["ABAAUFTYP"].ToString();
                    _krs = row["ABAAUFKRS"].ToString();
                    _lfd = row["ABAAUFLFD"].ToString();
                    _pos = row["ABAAUFPOS"].ToString();
                    key = _krs + _lfd + _pos + ";";
                    if (ABAAUFTYP == "N")
                        continue;
                    if (processed.Contains(key))
                        continue;
                    processed += key;
                    dwData["ABAVKAUFKRS"] = _krs;
                    dwData["ABAVKAUFLFD"] = _lfd;
                    dwData["ABAVKAUFPOS"] = _pos;
                    SResult = INTEXDBC.QueryICC_ABAX(dwData, 4);
                    STemp.Merge(SResult);
                    dwData["ABFMANDANT"] = app.Mandant;
                    dwData["ABFAUFKRS"] = _krs;
                    dwData["ABFAUFLFD"] = _lfd;
                    dwData["ABFAUFPOS"] = _pos;
                    SResult = INTEXDBC.QueryICC_MKRF(dwData, 6);
                    //                    SResult.Tables[0].TableName = "BMKRF";
                    SReturn.Merge(SResult);
                    SResult = INTEXDBC.QueryABFOLGE(dwData, 1);
                    SReturn.Merge(SResult);
                    SResult = INTEXDBC.QueryKSLEIST(dwData, 3);
                    SReturn.Merge(SResult);
                    IsRun = true;
                }
                SReturn.Merge(STemp);
                if (!IsRun)
                    break;
            }
            return SReturn;
        }
        private DataSet JLTON(string _krs, string _lfd, string _pos, string _EXART)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataSet STemp = new DataSet();
            DataSet STemp1 = new DataSet();
            string FKRS = "", FLD = "", FPOS = "",FART="",FVAR="",FFARBE="",FFINISH="";
            bool IsHasAB55 = false, IsHasAB45 = false, IsHasAB05 = false;
            DataRow dwQueryTB = INTEXDBC.Convert2ICC_QUERYTB(null, app.company, false);
            DataTable tb = dwQueryTB.Table.Clone();
            DataRow dwData = tb.NewRow();
            dwData["EXART"] = _EXART;
            INTEXDBC.DeleteICC_QUERYTB(dwData, 2);
            DataRow dwWDECK = null;
            //获取后整理生产单
            dwData.ItemArray = dwQueryTB.ItemArray;
            dwData["VKPMANDANT"] = app.Mandant;
            dwData["VKPAUFKRS"] = _krs;
            dwData["VKPAUFLFD"] = _lfd;
            dwData["VKPAUFPOS"] = _pos;
            dwData["C2"] = "NO";
            dwData["EXART"] = _EXART;
            INTEXDBC.InsertICC_QUERYTB(dwData, 1);
            dwData["C1"] = "F";
            SResult = INTEXDBC.QueryWDECK(dwData, 22);

            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                dwData.ItemArray = dwQueryTB.ItemArray;
                dwWDECK = row;
                FKRS = dwWDECK["ABPAUFKRS"].ToString();
                FLD = dwWDECK["ABPAUFLFD"].ToString();
                FPOS = dwWDECK["ABPAUFPOS"].ToString();
                FART = dwWDECK["ABPART"].ToString();
                FVAR = dwWDECK["ABPVAR"].ToString();
                FFARBE = dwWDECK["ABPFARBE"].ToString();
                FFINISH = dwWDECK["ABPBESCHICH"].ToString();
                dwData["ABPMANDANT"] = app.Mandant;
                dwData["ABPAUFKRS"] = FKRS;
                dwData["ABPAUFLFD"] = FLD;
                dwData["ABPAUFPOS"] = FPOS;
                dwData["C2"] = "NO";
                dwData["EXART"] = _EXART;

                if (FLD == "000000")
                    continue;
                IsHasAB05 = true;
                INTEXDBC.InsertICC_QUERYTB(dwData, 1);//记录AB05
                dwData["C1"] = "R";
                STemp1 = INTEXDBC.QueryWDECK(dwData, 22);
                STemp.Merge(STemp1);
            }
            if (dwWDECK == null)
                return SReturn;
            #region===通过品种========
           
            dwData["ABPMANDANT"] = app.Mandant;
            dwData["ABPART"] = FART;
            dwData["ABPVAR"] = FVAR;
            dwData["ABPFARBE"] =FFARBE;
            dwData["ABPBESCHICH"] = FFINISH;
            STemp = INTEXDBC.QueryABPOS(dwData, 26);
            foreach (DataRow row in STemp.Tables[0].Rows)
            {
                dwData.ItemArray = dwQueryTB.ItemArray;
                dwData["ABPMANDANT"] = row["ABPMANDANT"];
                dwData["ABPAUFKRS"] = row["ABPAUFKRS"];
                dwData["ABPAUFLFD"] = row["ABPAUFLFD"];
                dwData["ABPAUFPOS"] = row["ABPAUFPOS"];
                dwData["C2"] = "NO";
                dwData["EXART"] = _EXART;
                INTEXDBC.InsertICC_QUERYTB(dwData, 1);//记录AB50  
            }
            SResult.Clear(); 
            SResult.Merge(STemp);
            STemp.Clear();
            dwData["ABPMANDANT"] = app.Mandant;
            dwData["ABPART"] = _EXART.Substring(0,7);
            dwData["ABPFARBE"] = "-"+Convert.ToInt16(_pos);
            STemp = INTEXDBC.QueryABPOS(dwData, 28);
            foreach (DataRow row in STemp.Tables[0].Rows)
            {
                dwData.ItemArray = dwQueryTB.ItemArray;
                dwData["ABPMANDANT"] = row["ABPMANDANT"];
                dwData["ABPAUFKRS"] = row["ABPAUFKRS"];
                dwData["ABPAUFLFD"] = row["ABPAUFLFD"];
                dwData["ABPAUFPOS"] = row["ABPAUFPOS"];
                dwData["C2"] = "NO";
                dwData["EXART"] = _EXART;
                INTEXDBC.InsertICC_QUERYTB(dwData, 1);//记录AB50  
            }
            #endregion           
            SResult.Merge(STemp);
            STemp.Clear();
            #region====通过可能的订单网络=========           
            if(!IsHasAB05)
                return SReturn;             
            dwData["ABPMANDANT"] = app.Mandant;
            dwData["ABPAUFKRS"]=FKRS;
            dwData["ABPAUFLFD"] =FLD;
            dwData["ABPAUFPOS"] = FPOS;
            dwData["C2"] = "NO";
            dwData["EXART"] = _EXART;
            INTEXDBC.InsertICC_QUERYTB(dwData, 1);//记录AB55
            STemp1 = INTEXDBC.QueryBAAUF(dwData, 6);
            STemp.Merge(STemp1);
            STemp1 = INTEXDBC.QueryGAAUF(dwData, 19);
            STemp.Merge(STemp1);
            IsHasAB55 = true;

            if (IsHasAB55)
            {
                SResult.Clear();
                SResult.Merge(STemp);
                STemp.Clear();
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwData.ItemArray = dwQueryTB.ItemArray;
                    dwData["ABPMANDANT"] = dwData["ABPMANDANT"];
                    dwData["ABPAUFKRS"] = dwData["ABPAUFKRS"];
                    dwData["ABPAUFLFD"] = dwData["ABPAUFLFD"];
                    dwData["ABPAUFPOS"] = dwData["ABPAUFPOS"];
                    dwData["C2"] = "NO";
                    dwData["EXART"] = _EXART;
                    INTEXDBC.InsertICC_QUERYTB(dwData, 1);//记录AB45/AB50 
                    STemp1 = INTEXDBC.QueryGAAUF(dwData, 20);
                    STemp.Merge(STemp1);
                    IsHasAB45 = true;
                }
            }
            if (IsHasAB45)
            {
                SResult.Clear();
                SResult.Merge(STemp);
                STemp.Clear();
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwData.ItemArray = dwQueryTB.ItemArray;
                    dwData["ABPMANDANT"] = dwData["ABPMANDANT"];
                    dwData["ABPAUFKRS"] = dwData["ABPAUFKRS"];
                    dwData["ABPAUFLFD"] = dwData["ABPAUFLFD"];
                    dwData["ABPAUFPOS"] = dwData["ABPAUFPOS"];
                    dwData["C2"] = "NO";
                    dwData["EXART"] = _EXART;
                    INTEXDBC.InsertICC_QUERYTB(dwData, 1);//记录AB50  
                }
            }
            #endregion

            return SReturn;
        }
        private void GetOrderNetWork(string _krs, string _lfd, string _pos)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataSet STemp = new DataSet();
            DataRow dwData = SetEnv.ICC_QUERYTB.NewRow();
            string EXART = _krs + "-" + Helpers.Helper.Right(_lfd, 4) + "-" + _pos;
            switch (app.company)
            {
                case "JLT":
                    JLTON(_krs, _lfd, _pos, EXART);
                    SResult = INTEXBL.QueryABKON(app.Mandant, _krs, _lfd, _pos, EXART);
                    SReturn.Merge(SResult);
                    dwData["EXART"] = EXART;
                    INTEXDBC.DeleteICC_QUERYTB(dwData, 2);
                    break;
                case "YGR":
                    SReturn = YGRON(_krs, _lfd, _pos, EXART);
                    break;
            }
            //然后，每个生产单，读取ICC_MKRF

            SetData.Clear();
            SetData.Merge(SReturn);
        }
        public void BuildData()
        {
            try
            {
                GetOrderNetWork(KRS, LFD, POS);
                SetEnv.ICC_MKRF.Clear();
                if (SetData.Tables.Contains("ICC_MKRF"))
                {
                    SetEnv.ICC_MKRF.Merge(SetData.Tables["ICC_MKRF"]);
                }
                if (SetData.Tables.Contains("ABFOLGE"))
                {
                    if (SetEnv.Tables.Contains("ABFOLGE"))
                    {
                        SetEnv.Tables["ABFOLGE"].Clear();
                    }
                    SetEnv.Merge(SetData.Tables["ABFOLGE"]);
                }
                if (SetData.Tables.Contains("KSLEIST"))
                {
                    if (SetEnv.Tables.Contains("KSLEIST"))
                    {
                        SetEnv.Tables["KSLEIST"].Clear();
                    }
                    SetEnv.Merge(SetData.Tables["KSLEIST"]);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}